  <template>
  <div>
    <search-tool :changePage="changePage" />
    <search-result
      ref="mySearchResult"
      :codes="codes"
      :total="total"
      :changePage="changePage"
      :pageSize="params.size"
      :deleteCodeById="deleteCodeById"
    />
  </div>
</template>

<script>
import SearchTool from './components/SearchTool.vue'
import SearchResult from './components/SearchResult.vue'
import {getCodeContent,addCodeContent,modifyCodeContent,deleteCodeContent} from '@/api/publish'
export default {
  name: 'ContentManage',
  data () {
    return {
      codes: [],
      total: 0,
      params: {
        body:{
          codeName:null
        },
        page: 1,
        size: 10
      }
    }
  },
  created () {
    this.codeList()
  },
  components: {
    SearchTool,
    SearchResult
  },
  methods: {
    // 搜索文章
    async codeList () {
      const result = await getCodeContent(this.params)
      this.total = result.data.total // 总记录数
      this.codes = result.data.records // 当前的数组
    },
    //分页变化
    changePage(newParams){
      this.params = {...JSON.parse(JSON.stringify(this.params)),...JSON.parse(JSON.stringify(newParams))}
      this.codeList()
    },
    //删除代码
    async deleteCodeById(id){
      const result = await deleteCodeContent(id)
      if(result.code==20000){
        this.$message({ type: 'success', message: '删除成功!' })
        this.params.page=1;
        this.codeList()
      }else {
        this.$message({ type: 'error', message: temp.message })
      }
    }
  }
}
</script>
